package com.vanward.mes.scan.controller;

import com.vanward.mes.scan.dto.BatchScanRequestDTO;
import com.vanward.mes.scan.dto.BatchScanResultDTO;
import com.vanward.mes.scan.dto.ScanResultDTO;
import com.vanward.mes.scan.dto.WorkOrderStatsDTO;
import com.vanward.mes.scan.dto.WorkshopLineTreeDTO;
import com.vanward.mes.scan.service.ScanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 扫码控制器
 */
@RestController
@RequestMapping("/api/scan")
public class ScanController {
    
    @Autowired
    private ScanService scanService;
    
    /**
     * 扫码接口
     * @param barcode 条码编号
     * @param scanType 扫码类型：IN（上线扫码）或 OUT（下线扫码）
     * @return 扫码结果
     */
    @PostMapping("/process")
    public ScanResultDTO processScan(@RequestParam String barcode,
                                   @RequestParam String scanType,
                                   @RequestParam(required = false) String lineId) {
        return scanService.processScan(barcode, scanType, lineId);
    }
    
    /**
     * 获取工单统计信息接口
     * @param barcode 条码编号
     * @return 工单统计信息
     */
    @PostMapping("/workOrderStats")
    public WorkOrderStatsDTO getWorkOrderStats(@RequestParam String barcode) {
        return scanService.getWorkOrderStats(barcode);
    }

    /**
     * 批量扫码接口
     * @param request 批量扫码请求
     * @return 批量扫码结果
     */
    @PostMapping("/batchProcess")
    public BatchScanResultDTO batchProcessScan(@RequestBody BatchScanRequestDTO request) {
        return scanService.processBatchScan(request);
    }
    
    /**
     * 查询车间产线树结构接口
     * @return 车间产线树结构列表
     */
    @GetMapping("/workshopLineTree")
    public List<WorkshopLineTreeDTO> getWorkshopLineTree() {
        return scanService.selectWorkshopLineTree();
    }
}
